package com.baigt.juc.base.count;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 类功能描述:
 * 下边的程序有什么问题？
 * 什么原因？
 * 怎么解决？
 *
 * @author baigt
 * @version V1.0
 * @since 2019-10-08 10:48
 */
public class _002CountExample4 {
    private static int threadTotal = 200;
    private static int clientTotal = 5000;

    private static long count = 0;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal; i++) {
            exec.execute(() -> {
                add();
                latch.countDown();
            });
        }
        latch.await();
        exec.shutdown();
        System.out.println("count:" + count);
    }

    private static synchronized void add() {
        count++;
    }

}
